<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.MediaVaultTest.UserStore" %>
<%@ page import="com.MediaVaultTest.Media" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="javax.jdo.JDOHelper" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.PersistenceManagerFactory" %>
<%@ page import="com.MediaVaultTest.PMF" %>
<%@ page import="javax.jdo.JDOObjectNotFoundException" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="com.MediaVaultTest.Format" %>
<%@ page import="com.MediaVaultTest.Sort" %>
<%@ page import="com.MediaVaultTest.QuotaLogic" %>
<%@ page import="com.MediaVaultTest.GetAdds" %>
<%

//Setup user service and data store
	 UserService userService = UserServiceFactory.getUserService();
	 User user = userService.getCurrentUser();
	 
	 PersistenceManager pm = PMF.get().getPersistenceManager();
	 //If user logged in
	 
	UserStore userStore = null;
	 if (user != null) {
		 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		 
		 //Check if user system
		 String email =  user.getEmail();
		 
		 
		 Key userkey = KeyFactory.createKey(UserStore.class.getSimpleName(), email);
		 try { 
			 userStore = pm.getObjectById(UserStore.class, userkey);
			 //See if anything is returned\
		 } catch (JDOObjectNotFoundException e) {
			 userStore = null;
		 }
		 
		
		 //if not create them
		 if(userStore==null){
			 int up = 0;
			 int down = 0;
			 int store = 0;
			 userStore = new UserStore(email, up, down, store);
			 //userStore = new UserStore();
			 userStore.setKey(userkey);
			 try {
				 pm.makePersistent(userStore);
			 } finally {
				 
			 }
		} else {
			
		}
	 }
	 
	//Check to make sure file is of acceptable type
		GetAdds getAdds= new GetAdds();
		String stringKey = request.getParameter("key");
		Key key = KeyFactory.stringToKey(stringKey);
		    
		//Get user the media file belongs to
		Format format = pm.getObjectById(Format.class, key);
		Media media = format.getMedia();
		String title = media.getTitle();
		String Content = "";
		String typeFormat = "";
		if(title.contains("mp3")){
			Content = "audio";
			typeFormat = "mp3";
		} else if(title.contains("ogv")) {
			Content = "video";
			typeFormat = "ogv";
		} else if(title.contains("oga")) {
			Content = "audio";
			typeFormat = "oga";
		} else if(title.contains("m4a")) {
			Content = "audio";
			typeFormat = "m4a";
		} else if(title.contains("m4v")) {
			Content = "video";
			typeFormat = "m4v";
		}
%>
<!-- Universal Head -->
 <html>
<head>
<title>QUT Media Vault</title>
  <meta name="description" content="website description" />
  <meta name="keywords" content="website keywords, website keywords" />
  <meta http-equiv="content-type" content="text/html; charset=windows-1252" />
  <link type="text/css" href="/static/jQuery.jPlayer.2.1.0/blue.monday/jplayer.blue.monday.css" rel="stylesheet" />
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script type="text/javascript" src="/static/jQuery.jPlayer.2.1.0/jquery.jplayer.min.js"></script>

  <% if(Content=="audio"){%>
   <script type="text/javascript">
    $(document).ready(function(){
      $("#jquery_jplayer_1").jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
        	mp3: "/serve?key=<%=request.getParameter("key") %>"
          });
        },
        swfPath: "/static/jQuery.jPlayer.2.1.0",
        supplied: "<%=typeFormat%>"
      });
    });
  </script>

  <% } else if(Content=="video") { %>
    <script type="text/javascript">
    $(document).ready(function(){
      $("#jquery_jplayer_1").jPlayer({
        ready: function () {
          $(this).jPlayer("setMedia", {
        	ogv: "/serve?key=<%=request.getParameter("key") %>"
          });
        },
        swfPath: "/static/jQuery.jPlayer.2.1.0",
        supplied: "<%=typeFormat%>"
      });
    });
  </script>
  <% } %>
</head>
<!-- Universal Body -->
<body><div id="main">
    <div id="header">
      <div id="logo">
        <div id="logo_text">
          <!-- class="logo_colour", allows you to change the colour of the text -->
          <h1><a href="index.jsp">Media Vault<span class="logo_colour">QUT</span></a></h1>
          <h2>Media Centre. Currently in Alpha</h2>
        </div>
        <div id="ads_top">
        	<%=
	        	getAdds.getTopBanner()
	        %>
        </div>
      </div>
      <div id="menubar">
        <ul id="menu">
          <!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
          <li><a href="/">General</a></li>
		  <li><a href="photos.jsp">Photos</a></li>
          <li><a href="music.jsp">Music</a></li>
          <li><a href="movies.jsp">Movies</a></li>
          <li><a href="tv_shows.jsp">TV Shows</a></li>
          <%if (userService.isUserAdmin()) {%>
          
		  <li><a href="adminPanel.jsp">Admin Panel</a></li>
		  <%} %>
         
		  <li><a href="userPanel.jsp">User Panel</a></li>
		  <li><a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">Sign Out</a></li>
        </ul>
      </div>
    </div>
    <div id="site_content">
    <div class="left_sidebar">
    <h1>Quota Remaining</h1>
    <table>
    <% QuotaLogic quotaLogic = new QuotaLogic(); %>
    <tr><td>Upload</td><td><%= quotaLogic.getMBUploadRemaining(userStore) %>MB</td></tr>
    <tr><td>Download</td><td><%= quotaLogic.getMBDownloadRemaining(userStore) %>MB</td></tr>
    <tr><td>Storage</td><td><%= quotaLogic.getMBStoreRemaining(userStore) %>MB</td></tr>
    </table>
	    </div>
      <div class="sidebar">
      <h1>Search</h1>
      <form method="post" action="#">
        <p>
          <input class="search" type="text" name="search_field" value="" />
          <input name="search" type="image" style="border: 0; margin: 0 0 -5px 0;" src="style/search.png" alt="Search" title="Search" />
          <select name="Field">
          <option>Title</option>
        </select>
        </p>
      </form>
        <div id="ads_left">
	        <%=
	        	getAdds.getRightBanner()
	        %>
        </div>
        
      </div>
      <div id="content">
      <% if(Content=="audio") { %>
     <div id="jquery_jplayer_1" class="jp-jplayer"></div>
  <div id="jp_container_1" class="jp-audio">
    <div class="jp-type-single">
      <div class="jp-gui jp-interface">
        <ul class="jp-controls">
          <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
          <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
          <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
          <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
          <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
          <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
        </ul>
        <div class="jp-progress">
          <div class="jp-seek-bar">
            <div class="jp-play-bar"></div>
          </div>
        </div>
        <div class="jp-volume-bar">
          <div class="jp-volume-bar-value"></div>
        </div>
        <div class="jp-time-holder">
          <div class="jp-current-time"></div>
          <div class="jp-duration"></div>
          <ul class="jp-toggles">
            <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
            <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
          </ul>
        </div>
      </div>
      <div class="jp-title">
        <ul>
          <li><%= media.getTitle() %></li>
        </ul>
      </div>
      <div class="jp-no-solution">
        <span>Update Required</span>
        To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
      </div>
    </div>
  </div>
  </div>
  <% } else if(Content=="video") { %>
  	  <div id="jp_container_1" class="jp-video ">
    <div class="jp-type-single">
      <div id="jquery_jplayer_1" class="jp-jplayer"></div>
      <div class="jp-gui">
        <div class="jp-video-play">
          <a href="javascript:;" class="jp-video-play-icon" tabindex="1">play</a>
        </div>
        <div class="jp-interface">
          <div class="jp-progress">
            <div class="jp-seek-bar">
              <div class="jp-play-bar"></div>
            </div>
          </div>
          <div class="jp-current-time"></div>
          <div class="jp-duration"></div>
          <div class="jp-controls-holder">
            <ul class="jp-controls">
              <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
              <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
              <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
              <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
              <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
              <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
            </ul>
            <div class="jp-volume-bar">
              <div class="jp-volume-bar-value"></div>
            </div>
            <ul class="jp-toggles">
              <li><a href="javascript:;" class="jp-full-screen" tabindex="1" title="full screen">full screen</a></li>
              <li><a href="javascript:;" class="jp-restore-screen" tabindex="1" title="restore screen">restore screen</a></li>
              <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
              <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
            </ul>
          </div>
          <div class="jp-title">
            <ul>
              <li><%= media.getTitle() %></li>
            </ul>
          </div>
        </div>
      </div>
      <div class="jp-no-solution">
        <span>Update Required</span>
        To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
      </div>
    </div>
  </div>
  <% }else { %>
   	<h2>The file format of <%=media.getTitle() %> is not supported</h2>
  	<h3>Currently only mp3 and ogv are supported</h3>
  <% } %>
    </div>
    <div id="footer">
      <p><a href="/">Home</a> | <a href="/">LOL Cats</a> | <a href="/">Contact Us</a></p>
      <p>Copyright &copy; IT CROWD | <a href="http://validator.w3.org/check?uri=referer">HTML5</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> | <a href="http://www.html5webtemplates.co.uk">design dervived from HTML5webtemplates.co.uk</a></p>
    </div>
  </div>
</body>
</html>

